package com.example.music;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class FuMusicActivity extends AppCompatActivity {


    ImageView musicback, left, right;
    public static ImageView pause, imgvoice;
    public static TextView texttile, voiceshow, filename;

    public static SeekBar seekBarvoice;
    private Context context;
    private boolean play = true;


    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fu_music);

        findView();


        if (MusicMain.back == 1) {
            System.exit(0);

        }

        seekBarvoice.setMax(MusicMain.voicemax);
        seekBarvoice.setProgress(MusicMain.thisvoice);

        voiceshow.setText("当前音量：" + MusicMain.thisvoice * 100 / MusicMain.voicemax + "%");

        seekBarvoice.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                MusicMain.audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, i, 0);
                MusicMain.thisvoice = MusicMain.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                MusicMain.seekBarvoice.setProgress(MusicMain.thisvoice);

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        texttile.setText(MusicMain.list.get(MusicMain.posion).getSongname(context));

        filename.setText(MusicMain.list.get(MusicMain.posion).filename);
        Log.d("ccc", filename.getText().toString());

        initView2();

    }

    private void initView2() {
        pause.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public void onClick(View view) {
                if (play == true) {
                    pause.setBackgroundResource(R.drawable.pause);
                    play = false;

                    MusicMain.pause.setBackgroundResource(R.drawable.pause);
                    MusicMain.mediaPlayer.pause();
                } else {
                    pause.setBackgroundResource(R.drawable.music_play);
                    MusicMain.pause.setBackgroundResource(R.drawable.music_play);
                    MusicMain.mediaPlayer.start();
                    play = true;

                }
            }
        });
        left.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public void onClick(View view) {

                if (MusicMain.list.size() == 0) {
                    Toast.makeText(FuMusicActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
                } else if (MusicMain.posion == 0) {

                    Toast.makeText(FuMusicActivity.this, "当前是第一首", Toast.LENGTH_SHORT).show();

                } else {
                    pause.setBackgroundResource(R.drawable.music_play);

                    MusicMain.playfu("left");
                    texttile.setText(MusicMain.list.get(MusicMain.posion).getSongname(context));
                    filename.setText(MusicMain.list.get(MusicMain.posion).filename);


                }
            }

        });
        right.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public void onClick(View view) {
                if (MusicMain.list.size() == 0) {
                    Toast.makeText(FuMusicActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();

                } else {
                    pause.setBackgroundResource(R.drawable.music_play);

                    MusicMain.playfu("right");
                    texttile.setText(MusicMain.list.get(MusicMain.posion).getSongname(context));
                    filename.setText(MusicMain.list.get(MusicMain.posion).filename);


                }

            }


        });
    }


    private void findView() {
        musicback = findViewById(R.id.musicback);
        left = findViewById(R.id.left);
        pause = findViewById(R.id.pause);
        right = findViewById(R.id.right);
        imgvoice = findViewById(R.id.imgvoice);
        texttile = findViewById(R.id.texttile);
        voiceshow = findViewById(R.id.voiceshow);
        filename = findViewById(R.id.filename);


        seekBarvoice = findViewById(R.id.seekBarvoice);


    }

}